package service

import (
	"dc-news/common/global/consts"
	myjwt "dc-news/common/middleware/jwt"
	"dc-news/common/resp"
	"dc-news/common/utils"
	"dc-news/dao"
	"dc-news/entity"
	"dc-news/vo"
	"github.com/gin-gonic/gin"
	"github.com/spf13/cast"
	"go.uber.org/zap"
	"net/http"
	"time"
)

// AddCommentService 添加评论
func AddCommentService(c *gin.Context, userId int, req *vo.AddCommentReq) {
	isMuted, err := dao.GetUserIsMutedById(userId)
	if err != nil {
		zap.S().Error("添加评论错误：", err)
		c.JSON(http.StatusOK, resp.NewErrResp(consts.ServerErrorCode, "服务器错误"))
		return
	}
	if isMuted {
		c.JSON(http.StatusOK, resp.NewErrResp(consts.FailCode, "你已被禁言"))
		return
	}
	comment := &entity.Comments{
		NewsId:         req.NewsId,
		UserId:         userId,
		ReplyCommentId: req.ReplyCommentId,
		Content:        req.Content,
		CommentTime:    time.Now().Unix(),
	}
	err = dao.CreateComment(comment)
	if err != nil {
		zap.S().Error("添加评论错误：", err)
		c.JSON(http.StatusOK, resp.NewErrResp(consts.ServerErrorCode, "服务器错误"))
		return
	}
	c.JSON(http.StatusOK, vo.NewAddCommentResp(comment.ID))
}

// GetCommentService 获取评论
func GetCommentService(c *gin.Context, id int) {
	claims := c.MustGet("claims").(*myjwt.CustomClaims)
	comment, err := dao.GetCommentById(id, cast.ToInt(claims.Id))
	if err != nil {
		zap.S().Error("获取评论错误：", err)
		c.JSON(http.StatusOK, resp.NewErrResp(consts.ServerErrorCode, "服务器错误"))
		return
	}
	c.JSON(http.StatusOK, vo.NewGetCommentResp(comment))
}

// GetNewsCommentService 获取新闻评论
func GetNewsCommentService(c *gin.Context, id int) {
	claims := c.MustGet("claims").(*myjwt.CustomClaims)
	comments, err := dao.GetNewsCommentsById(id, cast.ToInt(claims.Id))
	if err != nil {
		zap.S().Error("获取新闻评论错误：", err)
		c.JSON(http.StatusOK, resp.NewErrResp(consts.ServerErrorCode, "服务器错误"))
		return
	}
	c.JSON(http.StatusOK, vo.NewGetCommentsResp(comments))
}

// GetCommentCommentService 获取评论的评论
func GetCommentCommentService(c *gin.Context, id int) {
	claims := c.MustGet("claims").(*myjwt.CustomClaims)
	comments, err := dao.GetCommentCommentsById(id, cast.ToInt(claims.Id))
	if err != nil {
		zap.S().Error("获取评论的评论错误：", err)
		c.JSON(http.StatusOK, resp.NewErrResp(consts.ServerErrorCode, "服务器错误"))
		return
	}
	c.JSON(http.StatusOK, vo.NewGetCommentsResp(comments))
}

// LikesCommentService 赞/踩评论
func LikesCommentService(c *gin.Context, id int, userId int, req *vo.LikeCommentReq) {
	err := dao.SetCommentLikesByIdAndType(userId, id, req.Likes)
	if err != nil {
		if err.Error() == "has been liked" {
			c.JSON(http.StatusOK, resp.NewErrResp(consts.FailCode, "无法重复点赞/踩"))
			return
		} else if err.Error() == "comment is not exist" {
			c.JSON(http.StatusOK, resp.NewErrResp(consts.FailCode, "评论不存在"))
			return
		} else {
			zap.S().Error("赞/踩评论错误：", err)
			c.JSON(http.StatusOK, resp.NewErrResp(consts.ServerErrorCode, "服务器错误"))
			return
		}
	}
	c.JSON(http.StatusOK, resp.NewOkResp())
}

// CancelLikesCommentService 取消评论赞/踩
func CancelLikesCommentService(c *gin.Context, id int, userId int) {
	err := dao.CancelLikesCommentById(id, userId)
	if err != nil {
		zap.S().Error("取消赞/踩评论错误：", err)
		c.JSON(http.StatusOK, resp.NewErrResp(consts.ServerErrorCode, "服务器错误"))
		return
	}
	c.JSON(http.StatusOK, resp.NewOkResp())
}

// GetUserCommentsService 获取用户评论列表
func GetUserCommentsService(c *gin.Context, userId int) {
	p := utils.NewPagination(c)
	comments, err := dao.GetUserCommentListById(userId, p)
	if err != nil {
		zap.S().Error("获取用户评论列表错误：", err)
		c.JSON(http.StatusOK, resp.NewErrResp(consts.ServerErrorCode, "服务器错误"))
		return
	}
	c.JSON(http.StatusOK, vo.NewGetUserCommentListResp(comments, p))
}

// GetUserLikeCommentsListService 获取用户点赞的评论
func GetUserLikeCommentsListService(c *gin.Context, userId int) {
	p := utils.NewPagination(c)
	comments, err := dao.GetUserLikesCommentListById(userId, p)
	if err != nil {
		zap.S().Error("获取用户评论列表错误：", err)
		c.JSON(http.StatusOK, resp.NewErrResp(consts.ServerErrorCode, "服务器错误"))
		return
	}
	c.JSON(http.StatusOK, vo.NewGetUserCommentListResp(comments, p))
}
